<template>
  <div class="app-container">
    <el-dialog :title="pageTitle" :visible.sync="dialogFormVisible" :before-close="beforeClose">
      <el-form :model="form">
        <el-form-item label="权限名称：" :label-width="formLabelWidth">
          <el-input v-model="form.title" minlength="2" maxlength="10" placeholder="请输入权限名称" />
        </el-form-item>
        <el-form-item label="接口地址：" :label-width="formLabelWidth">
          <el-input v-model="form.url" placeholder="请输入接口地址" />
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="选择主菜单：" :label-width="formLabelWidth">
              <el-select v-model="form.parent_menu_id" placeholder="请选择主菜单" @change="change">
                <el-option
                  v-for="item in parentMenus"
                  :key="item.id"
                  :label="item.title"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="选择子菜单：" :label-width="formLabelWidth">
              <el-select v-model="form.menu_id" placeholder="请选择主菜单">
                <el-option
                  v-for="item in childMenus"
                  :key="item.id"
                  :label="item.title"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="canel">取 消</el-button>
        <el-button type="primary" @click="sure">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { Message } from 'element-ui'
import { getFirstMenu, getSecondMenu } from '@/api/menu'
import { handle } from '@/api/rule'
export default {
  name: 'RuleHandle',
  data() {
    return {
      form: {
        id: 'new',
        title: '',
        url: '',
        parent_menu_id: '',
        menu_id: ''
      },
      formLabelWidth: '120px',
      dialogFormVisible: false,
      parentMenus: [],
      childMenus: [],
      pageTitle: '添加权限'
    }
  },

  methods: {
    beforeClose() {
      this.canel()
    },

    add() {
      this.getFirstMenu()
      this.form.id = 'new'
      this.pageTitle = '添加权限'
      this.dialogFormVisible = true
    },

    edit(item) {
      this.form.id = item.id
      this.form.title = item.title
      this.form.url = item.url
      this.form.menu_id = item.menu_id
      this.form.parent_menu_id = item.parent_menu_id
      this.pageTitle = '编辑权限'
      this.dialogFormVisible = true
      this.getFirstMenu()
      this.getSecondMenu(item.parent_menu_id)
    },

    canel() {
      this.dialogFormVisible = false
      this.form.id = 'new'
      this.form.title = ''
      this.form.url = ''
      this.form.menu_id = ''
      this.form.parent_menu_id = ''
      this.parentMenus = []
      this.childMenus = []
    },

    sure() {
      handle(this.form).then(res => {
        Message({
          message: res.data.info,
          type: 'success',
          duration: 2000
        })
        this.$emit('afterSave')
        this.canel()
      })
    },

    change(id) {
      this.childMenus = []
      this.form.menu_id = ''
      this.getSecondMenu(id)
    },

    getFirstMenu() {
      getFirstMenu({}).then(res => {
        this.parentMenus = res.data
      })
    },

    getSecondMenu(id) {
      getSecondMenu({ pid: id }).then(res => {
        this.childMenus = res.data
      })
    }
  }
}
</script>
